package datastructure;

/**
 * @Author leecho
 * @Date: 2021/10/11 9:50
 * @Version 1.0
 */

public class JosefLinkDemo {
    public static void main(String[] args) {
        JosefLink josefLink = new JosefLink();
        josefLink.addJosefNode(3);
        josefLink.listJosefLink(josefLink.getFirst());
    }
}

class JosefLink{
    /*
    * 思路
    * - 创建一个头结点
    * - 加入新的节点的时候，让新的节点指向头结点
    *   - 创建一个临时变量来遍历链表，添加节点的时候添加到当前链表的后面并让新的节点指向头部
    * */
    private JosefNode first = null;

    public JosefNode getFirst() {
        return first;
    }

    public void addJosefNode(int nums){
        JosefNode curBoy = null;
        for (int i = 0; i < nums; i++) {
            JosefNode boy = new JosefNode(i);
            if (i == 0){
                first = boy;
                first.setNext(first);
                curBoy = first;
            }else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = curBoy.getNext();
            }
        }
    }

    public void listJosefLink(JosefNode first){
        JosefNode temp = first;
        while (true){
            System.out.println(temp.getNo());
            if (temp.getNext() == first){
                break;
            }
            temp = temp.getNext();
        }
    }
}

class JosefNode{
    private int no;
    private JosefNode next;

    public JosefNode(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public JosefNode getNext() {
        return next;
    }

    public void setNext(JosefNode next) {
        this.next = next;
    }
}

